<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">
          <a-col :span="12">
            <a-form-model-item label="流程状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bpmStatus">
              <a-input :disabled=formDisabled v-model="model.bpmStatus" :max-length="36"  placeholder="请输入流程状态"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="流程标题" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bpmBizTitle">
              <a-input :disabled=formDisabled v-model="model.bpmBizTitle" :max-length="36"  placeholder="请输入流程标题"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="单据编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="billNo">
              <a-input :disabled=formDisabled v-model="model.billNo" :max-length="36"  placeholder="请输入单据编号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="申请人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="employeeId">
              <a-input :disabled=formDisabled v-model="model.employeeId" :max-length="36"  placeholder="请输入申请人"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="预计到岗时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="arrivalTime">
              <j-date :disabled=formDisabled placeholder="请选择预计到岗时间" v-model="model.arrivalTime"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="是否外派" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isExpatriate">
              <j-dict-select-tag :disabled=formDisabled type="list" v-model="model.isExpatriate" dictCode="yes_or_no" placeholder="请选择是否外派" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="调整前部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="beforeDepartId">
              <a-input :disabled=formDisabled v-model="model.beforeDepartId" :max-length="36"  placeholder="请输入调整前部门"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="调整前岗位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="beforePostId">
              <a-input :disabled=formDisabled v-model="model.beforePostId" :max-length="36"  placeholder="请输入调整前岗位"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="调整前所属团队" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="beforeTeamId">
              <a-input :disabled=formDisabled v-model="model.beforeTeamId" :max-length="36"  placeholder="请输入调整前所属团队"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="调整前薪级" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="beforeSalaryScale">
              <a-input :disabled=formDisabled v-model="model.beforeSalaryScale" :max-length="36"  placeholder="请输入调整前薪级"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="调整前车贴（元）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="beforeVehicleSubsidies">
              <a-input-number :disabled=formDisabled :max="999999999" :min="0" :precision="2" v-model="model.beforeVehicleSubsidies" placeholder="请输入调整前车贴（元）" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="调整后部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="afterDepartId">
              <a-input :disabled=formDisabled v-model="model.afterDepartId" :max-length="36"  placeholder="请输入调整后部门"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="调整后岗位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="afterPostId">
              <a-input :disabled=formDisabled v-model="model.afterPostId" :max-length="36"  placeholder="请输入调整后岗位"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="调整后所属团队" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="afterTeamId">
              <a-input :disabled=formDisabled v-model="model.afterTeamId" :max-length="36"  placeholder="请输入调整后所属团队"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="调整后薪级" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="afterSalaryScale">
              <a-input :disabled=formDisabled v-model="model.afterSalaryScale" :max-length="36"  placeholder="请输入调整后薪级"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="调整后车贴（元）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="afterVehicleSubsidies">
              <a-input :disabled=formDisabled v-model="model.afterVehicleSubsidies" :max-length="36"  placeholder="请输入调整后车贴（元）"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="调整原因" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="adjustReason">
              <a-textarea :disabled=formDisabled v-model="model.adjustReason" rows="4" :max-length="500" placeholder="请输入调整原因" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="工作交接" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isWorkHandover">
              <j-dict-select-tag :disabled=formDisabled type="radio" v-model="model.isWorkHandover" dictCode="yes_or_no" placeholder="请选择工作交接" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="工作交接人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="workHandoverPerson">
              <select-user-by-dep   :multi="false" :disabled=formDisabled v-model="model.workHandoverPerson" ></select-user-by-dep>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="办公设备交接情况" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isOfficeSupplies">
              <j-dict-select-tag :disabled=formDisabled type="radio" v-model="model.isOfficeSupplies" dictCode="yes_or_no" placeholder="请选择办公设备交接情况" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="办公设备交接人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="officeSuppliesPerson">
              <select-user-by-dep   :multi="false" :disabled=formDisabled v-model="model.officeSuppliesPerson" ></select-user-by-dep>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="有无借款" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isLoan">
              <j-dict-select-tag :disabled=formDisabled type="radio" v-model="model.isLoan" dictCode="yes_or_no" placeholder="请选择有无借款" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="借款金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="loanMoney">
              <a-input-number :disabled=formDisabled :max="999999999" :min="0" :precision="2" v-model="model.loanMoney" placeholder="请输入借款金额" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea :disabled=formDisabled v-model="model.remark" rows="4" :max-length="500" placeholder="请输入备注" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="附件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="attachment">
              <j-upload :disabled=formDisabled v-model="model.attachment"   ></j-upload>
            </a-form-model-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import SelectUserByDep from '@/views/scy/components/SelectUserByDep'
  import SelectTeamInput from '@/views/scy/components/SelectTeamInput'
  import SelectCompanyInput from '@/views/scy/components/SelectCompanyInput'
  import SelectCustomerInput from '@/views/scy/components/SelectCustomerInput'
  import moment from 'moment'
  import { mapGetters } from 'vuex'

  export default {
    name: 'ErpHrEmployeeAdjustPostBillForm',
    components: {
      SelectUserByDep,
      SelectTeamInput,
      SelectCompanyInput,
      SelectCustomerInput
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 24 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/api/erp/hr/adjust/erpHrEmployeeAdjustPostBill/add",
          edit: "/api/erp/hr/adjust/erpHrEmployeeAdjustPostBill/edit",
          queryById: "/api/erp/hr/adjust/erpHrEmployeeAdjustPostBill/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      ...mapGetters(['userInfo']),
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.$refs.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
                that.$emit('unButtonDisable');
              }
            }).catch((error)=>{
               that.$emit('unButtonDisable');
             }).finally(() => {
               that.confirmLoading = false;
             })
          }else {
           that.$emit('unButtonDisable');
          }

        })
      },
    }
  }
</script>
